import { TODO_DEL, TODO_CHECK, TODO_ADD } from './actiontype';
export const delTodoActionCreator = (id) => ({
  type: TODO_DEL,
  payload: id,
});

export const okTodoActionCreator = (id) => ({
  type: TODO_CHECK,
  payload: id,
});

// 模拟异步Api的方法
function loadNewTodoAPI(params) {
  const newTask = '学习异步Action';
  console.log('2秒后请求成功');
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log('请求成功,数据返回');
      resolve(newTask);
    }, 2000);
  });
}

export const addTodoActionCreator = (task) => {
  return async (dispath) => {
    const res = await loadNewTodoAPI();
    console.log(res);
    dispath({
      type: TODO_ADD,
      payload: task,
    });
  };
};
